package cn.iocoder.an.module.mold.controller.admin.recheckstatics;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.recheckstatics.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.recheckstatics.RecheckStaticsDO;
import cn.iocoder.an.module.mold.service.recheckstatics.RecheckStaticsService;

@Tag(name = "管理后台 - 复检统计")
@RestController
@RequestMapping("/mold/recheck-statics")
@Validated
public class RecheckStaticsController {

    @Resource
    private RecheckStaticsService recheckStaticsService;

    @PostMapping("/create")
    @Operation(summary = "创建复检统计")
    @PreAuthorize("@ss.hasPermission('mold:recheck-statics:create')")
    public CommonResult<Long> createRecheckStatics(@Valid @RequestBody RecheckStaticsSaveReqVO createReqVO) {
        return success(recheckStaticsService.createRecheckStatics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新复检统计")
    @PreAuthorize("@ss.hasPermission('mold:recheck-statics:update')")
    public CommonResult<Boolean> updateRecheckStatics(@Valid @RequestBody RecheckStaticsSaveReqVO updateReqVO) {
        recheckStaticsService.updateRecheckStatics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除复检统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-statics:delete')")
    public CommonResult<Boolean> deleteRecheckStatics(@RequestParam("id") Long id) {
        recheckStaticsService.deleteRecheckStatics(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除复检统计")
                @PreAuthorize("@ss.hasPermission('mold:recheck-statics:delete')")
    public CommonResult<Boolean> deleteRecheckStaticsList(@RequestParam("ids") List<Long> ids) {
        recheckStaticsService.deleteRecheckStaticsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得复检统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-statics:query')")
    public CommonResult<RecheckStaticsRespVO> getRecheckStatics(@RequestParam("id") Long id) {
        RecheckStaticsDO recheckStatics = recheckStaticsService.getRecheckStatics(id);
        return success(BeanUtils.toBean(recheckStatics, RecheckStaticsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得复检统计分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-statics:query')")
    public CommonResult<PageResult<RecheckStaticsRespVO>> getRecheckStaticsPage(@Valid RecheckStaticsPageReqVO pageReqVO) {
        PageResult<RecheckStaticsDO> pageResult = recheckStaticsService.getRecheckStaticsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckStaticsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出复检统计 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-statics:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecheckStaticsExcel(@Valid RecheckStaticsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckStaticsDO> list = recheckStaticsService.getRecheckStaticsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "复检统计.xls", "数据", RecheckStaticsRespVO.class,
                        BeanUtils.toBean(list, RecheckStaticsRespVO.class));
    }

}